package com.tebiecloud.base.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tebiecloud.base.client.model.entity.BaseAccountLogs;
import com.tebiecloud.base.server.mapper.BaseAccountLogsMapper;
import com.tebiecloud.base.server.service.BaseAccountLogsService;
import com.tebiecloud.common.mybatis.base.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * 登录日志 服务实现类
 * @author tebie
 * @date 2019-08-01
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class BaseAccountLogsServiceImpl extends BaseServiceImpl<BaseAccountLogsMapper, BaseAccountLogs> implements BaseAccountLogsService {

    @Autowired
    private BaseAccountLogsMapper baseAccountLogsMapper;

    /**
     * 添加登录日志
     * @param log
     */
    @Override
    public boolean save(BaseAccountLogs log) {
        QueryWrapper<BaseAccountLogs> queryWrapper = new QueryWrapper();
        queryWrapper.lambda()
                .eq(BaseAccountLogs::getAccountId, log.getAccountId())
                .eq(BaseAccountLogs::getUserId, log.getUserId());
        int count = baseAccountLogsMapper.selectCount(queryWrapper);
        log.setLoginNums(count + 1);
        log.setLoginTime(new Date());

        return super.save(log);
    }

}
